<template>
  <div style="padding: 20px;line-height: 3.5">

  </div>
</template>

<script setup lang="ts" name="paramsCommonDetails">
import {useRoute} from 'vue-router';
import {defineAsyncComponent, reactive, onMounted, watch, nextTick, onActivated, ref} from 'vue';
import axios from "axios";
// 定义变量内容
const route = useRoute();
onMounted(() => {
  get_emp_emergency_arm();
  get_history_elec_emgency_plane();
})
const state = reactive({
  emengency_state: "",
  select_emgency: "",
  emengencyoptions: [],
  baseurl: "http://106.53.97.117",
  empselect_emgency: "",
  empselect_emgency_log: "",
  tableData: [{
    date: '2016-05-02',
    name: '王小虎',
    address: '上海市普陀区金沙江路 1518 弄'
  }, {
    date: '2016-05-04',
    name: '王小虎',
    address: '上海市普陀区金沙江路 1517 弄'
  }, {
    date: '2016-05-01',
    name: '王小虎',
    address: '上海市普陀区金沙江路 1519 弄'
  }, {
    date: '2016-05-03',
    name: '王小虎',
    address: '上海市普陀区金沙江路 1516 弄'
  }]
})
const formatter = (row, column) => {
  return row.address;
}
//触发函数
const get_emp_emergency_arm = async () => {
  const respone = await axios.get("http://106.53.97.117:3060/EmergencyAlert/EmergencyAlert");
  state.emengency_state = respone.data.data
}
//触发获取当前的计划测试案例
const get_history_elec_emgency_plane = async () => {
  const respone = await axios.get(state.baseurl + ":2088/sql?sql=select * from elec_plan_start")
  for (const item of respone.data.data) {
    state.emengencyoptions.push({
      value: item.start_time + " " + item.plan_name,
      lable: item.plan_name + item.start_time,
      key: item.id
    })
  }
}

//触发选择项
const get_emergency_info = (e: any) => {
  state.empselect_emgency = e;
}
const start_emgency = async () => {
//  请求该测试的接口 加日期
  let str = state.empselect_emgency.split(" ")
  str.splice(2);
  let start_timestr = str.join(" ");
  var start_time = new Date(start_timestr);
  start_time.setMinutes(start_time.getMinutes() + 5); // Add 5 minutes
// 将日期转回字符串格式，注意需要+1因为JavaScript的月份是0-11
  var year = start_time.getFullYear();
  var month = ("0" + (start_time.getMonth() + 1)).slice(-2); // slice是为了保证两位数
  var day = ("0" + start_time.getDate()).slice(-2);
  var hour = ("0" + start_time.getHours()).slice(-2);
  var minute = ("0" + start_time.getMinutes()).slice(-2);
  var second = ("0" + start_time.getSeconds()).slice(-2);

  let end_timestr = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
  console.log(start_timestr)
  console.log(end_timestr)
  // const respone = await axios.get(state.baseurl+`:3060/EmergencyAlert/EmergencyAlertstream?start_time=${start_timestr}&end_time=${end_timestr}`)
  const respone = await axios.get(`http://106.53.97.117:3060/EmergencyAlert/EmergencyAlertstream?start_time=${start_timestr}&end_time=${end_timestr}`)
  state.empselect_emgency_log = respone.data
  let count = respone.data.reduce((acc, cur) => {
    cur.result.forEach(item => {
      let name = item.ent_name;
      if (name in acc) {
        acc[name] += 5;

      } else {
        acc[name] = 5;
      }
    });
    return acc;
  }, {});
  // 遍历上一步统计得到的对象，并将其转换为指定格式
  let resul_dict = []
  for (let name in count) {
    resul_dict.push({
      "name": name,
      "delay_time": count[name]
    });
  }
  console.log(resul_dict)

}
</script>
